package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @created 2016-2-22 14:08:56
 * @copyRight http://www.qiyunxin.com
 */
public class CreateCompanyActivity extends BaseActivity {

    private EditText edittext;
    private CompanyHandle mCompanyHandle = new CompanyHandle();
    private int entid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_add_company_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        if (getIntent() != null && getIntent().hasExtra("entname")) {
            entid = getIntent().getIntExtra("entid", 0);
            String entname = getIntent().getStringExtra("entname");
            edittext.setText(entname);
            edittext.setSelection(entname.length());
        }
        initListener();
        Utils.showSoftKeyboard(edittext);
    }

    private void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.add_company);
        if (entid != 0) {
            ((TextView) findViewById(R.id.title_textview))
                    .setText(R.string.editor_company);
        }
        edittext = (EditText) findViewById(R.id.edittext);
        ((TextView) findViewById(R.id.title_right_tv)).setText(R.string.save);
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
    }

    private void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Utils.hideSoftKeyboard(edittext);
                        finish();
                    }
                });
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        String companyName = edittext.getText().toString();
                        if (!TextUtils.isEmpty(companyName)) {
                            createCompany(companyName);
                        }
                    }
                });
    }

    private void createCompany(String companyName) {
        mCompanyHandle.createCompany(entid, companyName,
                new CompanyHandle.ICompanyResultListener() {

                    @Override
                    public void onResult(int status, String msg) {
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            sendBroadcast(new Intent(
                                    BroadcastAction.ACTION_REFRESH_ORG));
                            Utils.hideSoftKeyboard(edittext);
                            finish();
                        } else {
                            if (!TextUtils.isEmpty(msg)) {
                                IMApplication.showToast(msg);
                            }
                        }
                    }
                });
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

}
